package org.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.reggie.entry.ShoppingCart;
import org.example.reggie.result.ResultData;
import org.example.reggie.service.ShoppingCartService;
import org.example.reggie.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车 controller控制类
 */
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public ResultData<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        Long userId  = (Long) ThreadLocalUtil.getMap().get("id");
        shoppingCart.setUserId(userId);
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        if(null != dishId){
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }
        if(null != setmealId){
            queryWrapper.eq(ShoppingCart::getSetmealId,setmealId);
        }
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
        if(null !=cart){
            Integer count = cart.getNumber();
            cart.setNumber(count + 1);
            shoppingCartService.updateById(cart);
        }else{
            //尚未存在购物车，就添加到购物车
            shoppingCart.setNumber(1);
            /*
                insert into shopping_cart (ShoppingCart解析出来的字段) values (ShoppingCart解析出来的数据)
            */
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            //因为这个分支的cartServiceOne是null，所以要覆盖一下
            cart = shoppingCart;
        }
        return ResultData.success(cart);
    }
    @GetMapping("/list")
    public ResultData<List> list(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper =new LambdaQueryWrapper();
        queryWrapper.eq(null!=ThreadLocalUtil.getMap().get("id"),ShoppingCart::getUserId,ThreadLocalUtil.getMap().get("id"));
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return ResultData.success(list);
    }

    /**
     * 一次性清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public ResultData<String> clean(){
        //获取当前购物车用户Id
        Long userId = (Long) ThreadLocalUtil.getMap().get("id");
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, userId);
        shoppingCartService.remove(lambdaQueryWrapper);
        return ResultData.success("清空成功");
    }
}
